package com.zfl;

public class PathSum {

    private int pathCount;
    private long currentSum;


    public int pathSum(TreeNode root, int targetSum) {

        preorder(root,targetSum);
        return this.pathCount;
    }

    private void preorder(TreeNode root,int targetSum) {
        if (root==null) {
            return;
        }

        this.findPath(root,targetSum);
        this.currentSum=0;
        this.preorder(root.left,targetSum);
        this.preorder(root.right,targetSum);
    }

    private void findPath(TreeNode root,int targetSum) {
        if (root==null) {
            return;
        }
        this.currentSum+=root.val;
        if (currentSum==targetSum) {
            this.pathCount++;
        }
        findPath(root.left,targetSum);
        findPath(root.right,targetSum);
        this.currentSum-=root.val;
    }
}
